dd.mojo.extend({
    init:function()
    {
        // Yea... I can Explain... guess not.
        window.Mojo = {};
        window.Mojo.relaunch = function()
        {
            dd.mojo.toggleAppMenu();
        };
        window.Mojo._mojoManualStageReady = true;
        window.PalmSystem.stageReady();
        // The rest is normal
        dd.mojo.scene("index");
        dd(window).event({
            onKeyUp:function()
            {
                if(this.keyCode == 27)
                {
                    if(dd.mojo.scene._current == "index")
                        return true;
                    else
                        dd.mojo.back();
                }
                return false;
            },
            onMouseMove:function()
            {
                var ny = this(dd.html.body).top + this.mouse.changeY;
                var ty = this(dd.html.body).height - this.window.height;
                ty = ty >= 0 ? ty : 0;
                if(ny < 0 && -ny < ty)
                    this(dd.html.body).top += this.mouse.changeY;
                else if(-ny >= ty)
                    this(dd.html.body).top = -ty;
                else
                    this(dd.html.body).top = 0;
            }
        });
        dd.mojo.scene.extend = function(o)
        {
            for(i in o)
                this[i] = o[i];
        };
    },
    scripts:[],
    styles:[],
    scene:function(scene, parameters)
    {
        if(typeof(scene) == "string")
        {
            if(typeof(parameters) == "undefined")
                parameters = {};
            if(typeof(parameters.back) == "undefined")
                parameters.back = false;
            if(parameters.back === true)
                dd.mojo.animateForward();
            else
                dd.mojo.animateBackward();
            dd.xml({
                url: "scene/" + scene + "/" + scene + ".xml",
                onSuccess:function()
                {
                    dd.mojo.scene._current = scene;
                    dd(dd.html.body).clearElements();
                    dd(dd.html.body).top = 0;
                    for(var i in dd.mojo.styles)
                        dd.removeCSS(dd.mojo.styles[i]);
                    for(var i in dd.mojo.scripts)
                        dd.removeJS(dd.mojo.scripts[i]);
                    // Add Styles
                    for(var i in this.xml.style)
                    {
                        dd.mojo.styles.push(this.xml.style[i].getAttribute("src"));
                        dd.addCSS(this.xml.style[i].getAttribute("src"));
                    }
                    // Add Content
                    dd.mojo._dom.call(this, dd.html.body, this.xml.body[0]);
                    // Add Scripts
                    for(var i in this.xml.script)
                    {
                        dd.mojo.scripts.push(this.xml.script[i].getAttribute("src"));
                        dd.addJS(this.xml.script[i].getAttribute("src"));
                    }
                    // Application Menu
                    var am = dd(dd.html.body).addElement("div", {
                        "class":"webos-appmenu",
                        "id":"webos_appmenu"
                    });
                    dd(am).addElement("div", {
                        "class":"webos-appmenu-back",
                        "id":"webos_appmenu_back",
                        "onclick":"dd.mojo.toggleAppMenu()"
                    });
                    var ul = dd(am).addElement("ul", {
                        "class":"webos-appmenu-list",
                        "id":"webos_appmenu_list"
                    });
                    var items = this.xml.appmenu[0].item;
                    for(var i in items)
                    {
                        var c = "";
                        if(i == 0)
                            c += " webos-appmenu-first";
                        
                        if(items[i].getAttribute("disabled") == undefined)
                        {
                            c += " webos-appmenu-enabled";
                        }
                        else
                        {
                            if(items[i].getAttribute("disabled").toLowerCase() == "true")
                                c += " webos-appmenu-disabled";
                            else
                                c += " webos-appmenu-enabled";
                        }
                        c += " webos-appmenu-" + items[i].getAttribute("icon");
                        var li = dd(ul).addElement("li", {
                            "class":c
                        });
                        var a = dd(li).addElement("a", items[i].getAttribute("label"), {
                            "href":"#"
                        });
                        if(typeof(items[i].getAttribute("id")) != "undefined")
                            dd(a).setAttribute("id", items[i].getAttribute("id"));
                        //<a href="#" id="back" class="webos-menu-button-left webos-menu-icon-back"></a>
                    }
                    dd.mojo.scene.properties = parameters;
                    // Reset AppMenu
                    dd.mojo._appMenuHidden = true;
                    dd.mojo._appMenuTransition = false;
                    dd.mojo.oneInterval();
                },
                onFailure:function()
                {
                    dd(dd.html.body).addElement("div", "Invalid XML Scene");
                }
            });
        }
        else if(typeof(scene) == "object")
        {
            dd.mojo.scene.extend(scene);
            if("setup" in dd.mojo.scene)
                dd.mojo.scene.setup.call(this);
        }
    },
    oneInterval:function(i)
    {
        clearInterval(dd.mojo._oneInterval);
        if(typeof(i) != "undefined")
            dd.mojo._oneInterval = i;
    },
    _backScene:"index",
    _backArguments:{back:true},
    setBack:function(scene, arguments)
    {
        dd.mojo._backScene = scene;
        if(typeof(arguments) == "undefined")
        {
            dd.mojo._backArguments = {back:true};
        }
        else
        {
            dd.mojo._backArguments = arguments;
            dd.mojo._backArguments.back = true;
        }
        dd.mojo.back = function()
        {
            dd.mojo.scene.call(this, dd.mojo._backScene, dd.mojo._backArguments);
        }
    },
    animateForward:function()
    {
        var d = [320, 280, 210, 150, 100, 60, 30, 10, 0];
        var o = [0.2,  0.3,  0.4,  0.5,  0.6,  0.7, 0.8, 0.9, 1];
        for(var i in d)
        {
            setTimeout(dd.mojo.animateSceneHandle, i * 50, d[i], o[i]);
        }
    },
    animateBackward:function()
    {
        var d = [-320, -280, -210, -150, -100, -60, -30, -10, 0];
        var o = [0.2,  0.3,  0.4,  0.5,  0.6,  0.7, 0.8, 0.9, 1];
        for(var i in d)
        {
            setTimeout(dd.mojo.animateSceneHandle, i * 50, d[i], o[i]);
        }
    },
    animateSceneHandle:function(d, o)
    {
        dd(dd.html.body).left = d;
        dd(dd.html.body).opacity = o;
    },
    _appMenuHidden:true,
    _appMenuTransition:false,
    toggleAppMenu:function()
    {
        if(!dd.mojo._appMenuTransition)
        {
            dd.mojo._appMenuTransition = true;
            dd("webos_appmenu").display = "block";
            if(dd.mojo._appMenuHidden)
            {
                var d = [-320, -280, -210, -150, -100, -60, -30, -10, 0];
                var o = [0.0,  0.1,  0.2,  0.3,  0.4,  0.5, 0.6, 0.7, 0.8];
            }
            else
            {
                var d = [0, -10, -30, -60, -100, -150, -210, -280, -320];
                var o = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0];
            }
            for(var i in d)
            {
                setTimeout(dd.mojo.animateAppMenuHandle, i * 50, d[i], o[i], d.length - 1 == i);
            }
        }
    },
    animateAppMenuHandle:function(d, o, done)
    {
        dd("webos_appmenu_list").top = d;
        dd("webos_appmenu_back").e.style.background = "rgba(56, 46, 24, " + o + ")";
        if(done)
        {
            if(dd.mojo._appMenuHidden)
            {
                dd("webos_appmenu").display = "block";
                dd.mojo._appMenuHidden = false;
                dd.mojo._appMenuTransition = false;
            }
            else
            {
                dd("webos_appmenu").display = "none";
                dd.mojo._appMenuHidden = true;
                dd.mojo._appMenuTransition = false;
            }
        }
    },
    _dom:function(e, xml)
    {
        var tags = xml.children;
        for(var i in tags)
        {
            if(typeof(xml[tags[i].tagName][tags[i].index].innerText) == undefined)
                var ne = dd(e).addElement(tags[i].tagName, "");
            else
                var ne = dd(e).addElement(tags[i].tagName, xml[tags[i].tagName][tags[i].index].innerText, xml[tags[i].tagName][tags[i].index].attributes);
            if(typeof(xml[tags[i].tagName][tags[i].index].children) != undefined)
                dd.mojo._dom.call(this, ne, xml[tags[i].tagName][tags[i].index]);
        }
    },
    message:function(msg, options, callback)
    {
        
    },
    log:{
        info:function(message)
        {
            dd(dd.html.body).addElement("div", message, {
                "style":"background:#F1F1F1;padding:0 14px;border-bottom:1px solid #D4D4D4;"
            });
        },
        warn:function()
        {
            dd(dd.html.body).addElement("div", message, {
                "style":"background:#F1F1F1;padding:0 14px;border-bottom:1px solid #D4D4D4;"
            });
        },
        error:function()
        {
            dd(dd.html.body).addElement("div", message, {
                "style":"background:#F1F1F1;padding:0 14px;border-bottom:1px solid #D4D4D4;"
            });
        }
    }
});